<?php

namespace backend\controllers;

use Yii;
use common\models\Post;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Category;

/**
 * PostController implements the CRUD actions for Post model.
 */
class PostController extends Controller
{
    /**
     * @inheritdoc
     */
//    public function behaviors()
//    {
//        return [
//            'verbs' => [
//                'class' => VerbFilter::className(),
//                'actions' => [
//                    'delete' => ['POST'],
//                ],
//            ],
//        ];
//    }
    public function actionNews()
    {
        return $this->render('news');
    }
    public function actionAdmin(){
        $dataProvider = new ActiveDataProvider([
            'query' => Post::find(),
        ]);

        return $this->render('admin', [
            'dataProvider' => $dataProvider,
        ]);
    }
    public function actionAllpost()
    {
        $model = new Post();
        if (Yii::$app->request->isPost) {
            $model->load(Yii::$app->request->post());
            $type = Yii::$app->request->post()['Post']['type'];
            $condition = !empty($type) ? " type = " . $type : '';
        }
        if(!empty($condition)){
            $dataProvider = new ActiveDataProvider([
                'query' => Post::find()->where($condition),
            ]);
        }else{
            $dataProvider = new ActiveDataProvider([
                'query' => Post::find(),
            ]);
            $dataProvider->setSort(false);
        }
        return $this->render('allpost', [
            'dataProvider' => $dataProvider,
            'model' => $model,
        ]);
    }
    /**
     * Lists all Post models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Post::find(),
        ]);
        $model = new Post();
        if (Yii::$app->request->isPost) {
            $title = isset(Yii::$app->request->post()['Post']['title']) ? Yii::$app->request->post()['Post']['title'] : '';
            $condition = " title like '%" . $title . "%'";
            $condition .= " or type like '%" . $title . "%'";
            return $this->redirect(['searchpost','condition'=>$condition]);
//            print_r($title);die;
        }
        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'model' => $model,
        ]);
    }

    public function actionSearchpost($condition)
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Post::find()->where($condition),
        ]);
        return $this->render('search', [
                'dataProvider' => $dataProvider,
            ]
        );
    }

    /**
     * Displays a single Post model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $categorys = Category::find()->all();
        $category = [];
        foreach ($categorys as $k => $v){
            $category[$v->id] = $v->name;
        }
        return $this->render('view', [
            'model' => $this->findModel($id),
            'category' => $category,
        ]);
    }

    /**
     * Creates a new Post model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Post();

        if (Yii::$app->request->isPost) {
            $model->load(Yii::$app->request->post());
            $model->createat = date('Y-m-d H:m:s');
            $model->updateat = date('Y-m-d H:m:s');
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing Post model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Post model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['admin']);
    }

    /**
     * Finds the Post model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Post the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Post::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
